Building, Saving, and Loading a Keras Model

by CM


Posted on May 04, 2020



The Goal:

In this article, we will quickly explore how to build, save and load a Keras model.

Linear regression:

As an example, we will simply set up a linear regression model. Note this model will have a very bad accuracy and will just be used for the purpose of model saving / loading demonstration. In statistics, linear regression is a linear approach to modeling the relationship between a scalar response and one or more explanatory variables. The case of one explanatory variable is called simple linear regression.


Key components are:

Let's jump right into the Code. First, we import all required dependencies. (1) TensorFlow: is a free and open-source software library for dataflow and differentiable programming across a range of tasks. It is a symbolic math library, and is also used for machine learning applications such as neural networks. (2) Keras: is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano. It was developed with a focus on enabling fast experimentation. Being able to go from idea to result with the least possible delay is key to doing good research. (3) Numpy: is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.

### Importing all dependencies
import tensorflow as tf

import numpy as np

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

Second, we will build our dataset from scratch. In this regard, we will set up a training list: (1) one list with the x-values (2) one list with the y-values. After having our lists ready, we convert them into Tensors to be able to use them in our Keras model.

x_values = [1,2,3,4,5,6,7,8,9,10,11,12,13]
y_values = [1,4,9,16,25,36,49,64,81,100,121,144,169]

print(type(x_values))
print(type(y_values))

x_values = tf.constant(x_values)
y_values = tf.constant(y_values)

print(type(y_values))
print(type(x_values))

Let's have a look at the conversion.

==========================
OUTPUT
==========================

class 'list'
class 'list'
class 'tensorflow.python.framework.ops.EagerTensor'
class 'tensorflow.python.framework.ops.EagerTensor'

Next, we build our model using two dense layers.

model = Sequential()
model.add(tf.keras.layers.Dense(units = 101, input_shape=(1,)))
model.add(tf.keras.layers.Dense(units = 1, input_shape=(10,)))
model.summary()

We then compile our model respectively define the optimizer and loss function. After that we will start training our model using the x and y tensors we have already set up.

model.compile(optimizer="adam", loss="mean_squared_error")
model.fit(x_values, y_values, batch_size=1, epochs=100, verbose=1)

We can investigate the training -- note that this model is just a plain regression with limited validity.

==========================
OUTPUT
==========================

Epoch 1/100
13/13 [==============================] - 0s 1ms/step - loss: 6917.3574
Epoch 2/100
13/13 [==============================] - 0s 1ms/step - loss: 6602.9668
Epoch 3/100
13/13 [==============================] - 0s 1ms/step - loss: 6370.6440
Epoch 4/100
13/13 [==============================] - 0s 1ms/step - loss: 6097.8413
Epoch 5/100
13/13 [==============================] - 0s 1ms/step - loss: 5868.7236
....
Epoch 94/100
13/13 [==============================] - 0s 1ms/step - loss: 429.6958
Epoch 95/100
13/13 [==============================] - 0s 1ms/step - loss: 424.5723
Epoch 96/100
13/13 [==============================] - 0s 1ms/step - loss: 420.8812
Epoch 97/100
13/13 [==============================] - 0s 1ms/step - loss: 419.0024
Epoch 98/100
13/13 [==============================] - 0s 1ms/step - loss: 416.8020
Epoch 99/100
13/13 [==============================] - 0s 1ms/step - loss: 415.4740
Epoch 100/100
13/13 [==============================] - 0s 1ms/step - loss: 412.9370
tensorflow.python.keras.callbacks.History at 0x7f39343c1b70

Let us just throw in some number and see whether it gives us a quadratic prediction of our input.

test_tensor = [10]
print(type(test_tensor))

test_tensor = tf.constant(test_tensor)
prediction = model.predict(test_tensor)
print(prediction)

Ok the model does work / gives us a predictions back.

==========================
OUTPUT
==========================


[[100.48895]]

In order to save the full model. We can simply use the model.save function. model.save will save the model's architecture, weights, and training configuration in a single file/folder. This allows us to export a model so it can be used without access to the original Python code. Since the optimizer-state is recovered, you can resume training from exactly where you left off. Entire model can be saved in two different file formats (SavedModel and HDF5). It is to be noted that TensorFlow SavedModel format is the default file format in TF2.x. However, model can be saved in HDF5 format.

model.save('the_easy_model.h5')

The model should now be saved in your python workspace.

base_model.summary()

In order to recreate the model, we can use the keras load_model function. In order to do this -- make sure to add the respective dependency.

# Recreate the exact same model, including its weights and the optimizer
new_model = tf.keras.models.load_model('the_easy_model.h5')

# Show the model architecture
new_model.summary()

==========================
OUTPUT
==========================

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
dense_1 (Dense)              (None, 101)               202
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 102
=================================================================
Total params: 304
Trainable params: 304
Non-trainable params: 0
_________________________________________________________________

Works like a charm...

Yes, we did it! A quick explanation of saving and loading Keras models.

Transfer Learning

#EpicML


News
Dec 2021

--- Quantum ---

Simulating matter on the quantum scale with AI #Deepmind
Nov 2021

--- Graviton3 ---

Amazon announced its Graviton3 processors for AI inferencing - the next generation of its custom ARM-based chip for AI inferencing applications. #Graviton3
May 2021

--- Vertex AI & TPU Gen4. ---

Google announced its fourth generation of tensor processing units (TPUs) for AI and ML workloads and the Vertex AI managed platform #VertexAI #TPU
Feb 2021

--- TensorFlow 3D ---

In February of 2021, Google released TensorFlow 3D to help enterprises develop and train models capable of understanding 3D scenes #TensorFlow3D
Nov 2020

--- AlphaFold ---

In November of 2020, AlphaFold 2 was recognised as a solution to the protein folding problem at CASP14 #protein_folding
Oct 2019

--- Google Quantum ---

A research effort from Google AI that aims to build quantum processors and develop novel quantum algorithms to dramatically accelerate computational tasks for machine learning. #quantum_supremacy
Oct 2016

--- AlphaGo ---

Mastering the game of Go with Deep Neural Networks. #neural_network